/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package db.JDBC.core;

import core.*;
import core.comparators.ComparatorFactory;
import core.comparators.ShopComparator.SHOPCOMPARATORTYPE;
import db.JDBC.*;
import db.core.interfaces.ShopDAOCore;
import db.interfaces.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.TreeSet;
/**
 *
 * @author Anton
 */
public class ShopJdbcDAOCore implements ShopDAOCore
{
    private static ShopJdbcDAOCore instance = null;
    private ShopDAO shopDAO;
    private CRUDJdbcDAOCore crud;

    /**
     *
     */
    private ShopJdbcDAOCore()
    {
        JdbcDaoFactory daoFactory = JdbcDaoFactory.getInstance();
        shopDAO = daoFactory.getShopJdbcDAO();
        crud = CRUDJdbcDAOCore.getInstance();
    }

    /**
     * 
     * @return
     */
    public static ShopJdbcDAOCore getInstance()
    {
        if(instance == null)
            instance = new ShopJdbcDAOCore();
        return instance;
    }

    /**
     * 
     * @param id
     * @return
     */
    public Shop getShopById(long id)
    {
        return crud.getShop(id);
    }

    /**
     *
     * @param beginsWith
     * @param maxResult
     * @return
     */
    public Collection<String> searchForStringShops(String beginsWith, int maxResult)
    {
        Collection<Shop> names  = shopDAO.searchForShopsByName(beginsWith);
        Collection<String> t = new TreeSet<String>();
        List<String> returnCollection = new ArrayList<String>();
        if(names != null)
            for(Shop s : names)
                if(s != null)
                    t.add(s.getName());

        for(String s : t)
            if(s != null)
                returnCollection.add(s);
        
        if(returnCollection.size() < maxResult)
            maxResult = returnCollection.size();
        return returnCollection.subList(0, maxResult);
    }
    /**
     *
     * @param beginsWith
     * @return
     */
    public Collection<Shop> searchForObjectShops(String beginsWith)
    {
        Collection<Shop> names  = shopDAO.searchForShopsByName(beginsWith);
        Collection<Shop> t = new TreeSet<Shop>(ComparatorFactory.getShopComparator(SHOPCOMPARATORTYPE.ID, true));
        List<Shop> returnCollection = new ArrayList<Shop>();
        if(names != null)
            for(Shop s : names)
                if(s != null)
                    t.add(s);
        for(Shop s : t)
            if(s != null)
                returnCollection.add(s);
        Collections.sort(returnCollection, ComparatorFactory.getShopComparator(SHOPCOMPARATORTYPE.NAME, true));

        return returnCollection;
    }

    /**
     * 
     * @param name
     * @return
     */
    public Collection<Shop> getShopsByName(String name)
    {
        if(name == null)
            return null;
        Collection<Shop> shops = new ArrayList<Shop>();
        for(Shop s : shopDAO.getShopsByName(name))
            if(s != null)
                shops.add(crud.getShop(s.getId()));
        return shops;        
    }
    /**
     * 
     * @return
     */
    public Collection<Shop> getAllShops()
    {
        Collection<Shop> shops = new ArrayList<Shop>();
        Collection<Shop> dbShop = shopDAO.getAllShops();
        if(dbShop != null)
        {
            for(Shop s : shopDAO.getAllShops())
                if(s != null)
                    shops.add(crud.getShop(s.getId()));
        }
        return shops;        
    }
    /**
     * 
     * @param shop
     */
    public void addShop(Shop shop)
    {
        if(shop == null)
            return;
        crud.addShop(shop);
    }
    /**
     * 
     * @param shop
     */
    public void updateShop(Shop shop)
    {
        if(shop == null)
            return;
        crud.updateShop(shop);
    }
    /**
     * 
     * @param shop
     */
    public void deleteShop(Shop shop)
    {
        if(shop == null)
            return;
        crud.deleteShop(shop);
    }
}
